package com.sy.simpleegg.platform.kkb.loader

import android.annotation.SuppressLint
import com.blankj.utilcode.util.TimeUtils
import com.sy.simpleegg.platform.kkb.api.IDeviceConnectApi
import com.sy.simpleegg.platform.kkb.bean.device.ConnectTestEntity
import com.sy.simpleegg.platform.kkb.bean.device.DeviceUpdateEntity
import com.sy.simpleegg.platform.kkb.bean.device.HeartbeatEntity
import com.sy.simpleegg.platform.kkb.bean.device.RegisterEntity
import com.sy.simpleegg.platform.kkb.bean.device.RemoteDeviceConfigEntity
import com.sy.simpleegg.platform.kkb.bean.device.SubRegisterEntity
import com.sy.simpleegg.platform.kkb.network.ApiWrapper
import com.sy.simpleegg.platform.kkb.network.base.BaseLoader
import com.sy.simpleegg.platform.kkb.network.base.BaseResult
import io.reactivex.Observer

/**
 * @author sy
 * @date 2024/10/11 17:50
 * @desc
 */
class DeviceConnectLoader: BaseLoader() {
    @SuppressLint("CheckResult")
    fun connectTest(observer: Observer<BaseResult<ConnectTestEntity?>?>) {
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).connectTest()?.compose(
            ApiWrapper.singleInstance().applySchedulers(observer)
        )
    }

    @SuppressLint("CheckResult")
    fun pushDeviceError(map: Map<String?, Any?>, observer: Observer<BaseResult<Any?>?>) {
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).pushDeviceError(
            this.getYZYHeader(map),
            getParameter(map)
        )?.compose(ApiWrapper.singleInstance().applySchedulers(observer))
    }

    @SuppressLint("CheckResult")
    fun registerProduct(
        map: Map<String?, Any?>,
        observer: Observer<BaseResult<RegisterEntity?>?>,
    ) {
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).registerProduct(
            getHeaderYZHRegister(map),
            getParameter(map)
        )?.compose(ApiWrapper.singleInstance().applySchedulers(observer))
    }

    @SuppressLint("CheckResult")
    fun deviceHeartbeat(
        map: Map<String?, Any?>,
        observer: Observer<BaseResult<HeartbeatEntity?>?>,
    ) {
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).deviceHeartbeat(
            this.getYZYHeader(map),
            getParameter(map)
        )?.compose(ApiWrapper.singleInstance().applySchedulers(observer))
    }

    @SuppressLint("CheckResult")
    fun deviceUpInfo(map: Map<String?, Any?>, observer: Observer<BaseResult<HeartbeatEntity?>?>) {
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).deviceHeartbeat(
            this.getYZYHeader(map),
            getParameter(map)
        )?.compose(ApiWrapper.singleInstance().applySchedulers(observer))
    }

    @SuppressLint("CheckResult")
    fun checkUpdate(
        map: Map<String?, Any?>,
        obServer: Observer<BaseResult<DeviceUpdateEntity?>?>,
    ) {
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).checkUpdate(
            this.getYZYHeader(map),
            getParameter(map)
        )?.compose(
            ApiWrapper.singleInstance().applySchedulers(obServer)
        )
    }

    @SuppressLint("CheckResult")
    fun getDeviceConfig(
        map: Map<String?, Any?>,
        observer: Observer<BaseResult<RemoteDeviceConfigEntity?>?>,
    ) {
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).getDeviceConfig(
            this.getYZYHeader(map),
            getParameter(map)
        )?.compose(
            ApiWrapper.singleInstance().applySchedulers(observer)
        )
    }

    @SuppressLint("CheckResult")
    fun messageQueueResult(map: Map<String?, Any?>, observer: Observer<BaseResult<Any?>?>) {
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).mqResult(
            this.getYZYHeader(map),
            getParameter(map)
        )?.compose(ApiWrapper.singleInstance().applySchedulers(observer))
    }

    @SuppressLint("CheckResult")
    fun subDeviceRegister(
        map: Map<String?, Any?>,
        observer: Observer<BaseResult<SubRegisterEntity?>?>,
    ) {
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).subDeviceRegister(
            this.getYZYHeader(map),
            getParameter(map)
        )?.compose(ApiWrapper.singleInstance().applySchedulers(observer))
    }

    @SuppressLint("CheckResult")
    fun uploadDeviceInfo(
        map: Map<String?, Any?>,
        observer: Observer<BaseResult<HeartbeatEntity?>?>,
    ) {
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).uploadDeviceInfo(
            this.getYZYHeader(map),
            getParameter(map)
        )?.compose(ApiWrapper.singleInstance().applySchedulers(observer))
    }

    @SuppressLint("CheckResult")
    fun pushDeviceSet(map: MutableMap<String?, Any?>, observer: Observer<BaseResult<Any?>?>) {
        map["TimeStamp"] = TimeUtils.getNowMills()
        (ApiWrapper.singleInstance().setIsDebug(useTestUrl).getService(
            IDeviceConnectApi::class.java
        )).pushDeviceSet(
            this.getYZYHeader(map),
            getParameter(map)
        )?.compose(ApiWrapper.singleInstance().applySchedulers(observer))
    }
}